package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.entity.Dept;
import com.zrwl.poscloud.base.entity.DeptAdminR;
import com.zrwl.poscloud.base.entity.Grade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EBusiType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DeptUtil;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.zrwl.poscloud.xo.mapper.GradeMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 班级 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-02-27
 */
@Service
public class GradeServiceImpl extends SuperServiceImpl<GradeMapper, Grade> implements GradeService {

    @Autowired
    private GradeService gradeService;
    @Resource
    private GradeMapper gradeMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptAdminRService deptAdminRService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeAccountRecordService employeeAccountRecordService;

    @Override
    public IPage<GradeVO> getPageList(GradeVO gradeVO) {
        Page<Grade> page = new Page<>();
        page.setCurrent(gradeVO.getCurrentPage());
        page.setSize(gradeVO.getPageSize());
        IPage<GradeVO> pageList = gradeMapper.getPageList(page, gradeVO);
        List<GradeVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<GradeVO> getAllList(GradeVO gradeVO) {
        return gradeMapper.getAllList(gradeVO);
    }

    @Override
    public String add(GradeVO gradeVO, String adminUid) {
        if (StringUtils.isEmpty(gradeVO.getDeptUid()) || StringUtils.isEmpty(gradeVO.getDeptUid().trim())) {
            throw new RuntimeException("上级部门不可为空");
        }
        Dept dept = deptService.getById(gradeVO.getDeptUid());
        if (dept == null) {
            throw new RuntimeException("该上级部门不存在");
        }
        if (dept.getLevel() == 4) {
            throw new RuntimeException("该部门下不可存在子部门");
        }
        String deptUid = null;
        String deptCode = null;
        if (gradeVO.getDeptName().contains(",")) {
            String[] deptNameArray = gradeVO.getDeptName().split(",");

            for (String deptName : deptNameArray) {
                DeptVO deptVO = new DeptVO();
                deptVO.setName(deptName);
                deptVO.setCompanyUid(dept.getCompanyUid());
                deptVO.setSuperCompanyCode(dept.getCompanyCode());
                deptService.add(deptVO);
                if (StringUtils.isNotBlank(deptUid)) {
                    deptUid = deptUid + "," + deptVO.getUid();
                } else {
                    deptUid = deptVO.getUid();
                }
                if (StringUtils.isNotBlank(deptCode)) {
                    deptCode = deptCode + "," + deptVO.getCompanyCode();
                } else {
                    deptCode = deptVO.getCompanyCode();
                }
                Grade entity = new Grade();
                BeanUtils.copyProperties(gradeVO, entity, SysConf.STATUS);
                entity.setDeptUid(deptVO.getUid());
                entity.setBeginTime(new Date());
                entity.insert();
            }
        } else {
            DeptVO deptVO = new DeptVO();
            deptVO.setName(gradeVO.getDeptName());
            deptVO.setCompanyUid(dept.getCompanyUid());
            deptVO.setSuperCompanyCode(dept.getCompanyCode());
            deptService.add(deptVO);
            deptUid = deptVO.getUid();
            deptCode = deptVO.getCompanyCode();
            Grade entity = new Grade();
            BeanUtils.copyProperties(gradeVO, entity, SysConf.STATUS);
            entity.setDeptUid(deptVO.getUid());
            entity.setBeginTime(new Date());
            entity.insert();
            gradeVO.setUid(entity.getUid());
        }
        DeptAdminR deptAdminR = deptAdminRService.getByCompany(adminUid, dept.getCompanyUid());
        if (deptAdminR != null) {
            if (StringUtils.isNotBlank(deptAdminR.getDeptRoleTree()) && StringUtils.isNotBlank(deptUid)) {
                deptAdminR.setDeptRoleTree(deptAdminR.getDeptRoleTree() + "," + deptUid);
            }
            if (StringUtils.isNotBlank(deptAdminR.getDeptRoleTreeCode()) && StringUtils.isNotBlank(deptCode)) {
                deptAdminR.setDeptRoleTreeCode(deptAdminR.getDeptRoleTreeCode() + "," + deptCode);
            }
            deptAdminR.updateById();
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(GradeVO gradeVO) {
        Grade entity = gradeService.getById(gradeVO.getUid());
        BeanUtils.copyProperties(gradeVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(GradeVO gradeVO) {
        Grade entity = gradeService.getById(gradeVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public Grade getGradeByName(String companyCode, String deptName) {
        return gradeMapper.getGradeByName(DeptUtil.getLatestCompnay(companyCode), deptName);
    }

    @Override
    public Grade getGradeByDeptCode(String companyCode) {
        return gradeMapper.getGradeByDeptCode(companyCode);
    }

    @Override
    public String delClassAndDept(GradeVO gradeVO) {
        Grade entity = gradeService.getById(gradeVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        Dept dept = deptService.getById(entity.getDeptUid());
        dept.setStatus(EStatus.DISABLED);
        dept.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    @Transactional
    public String editLeave(GradeVO gradeVO, AdminVO adminVO) {
        String gradeUid = "";
        String deptUid = "";
        if (gradeVO.getGradeVOList().size() > 0) {
            for (GradeVO vo : gradeVO.getGradeVOList()) {
                if (StringUtils.isBlank(gradeUid)) {
                    gradeUid = vo.getUid();
                    deptUid = vo.getDeptUid();
                } else {
                    gradeUid = gradeUid + "," + vo.getUid();
                    deptUid = deptUid + "," + vo.getDeptUid();
                }
            }
        } else {
            gradeUid = gradeVO.getUid();
        }
        gradeMapper.updateLeaveByGradeUid(gradeUid);
        if (StringUtils.isNotBlank(deptUid)) {
            List<EmployeeVO> list = employeeService.getEmployeeListByDeptUid(deptUid);
            for (EmployeeVO employeeVO : list) {
                EmployeeAccountRecordVO employeeAccountRecordVO = new EmployeeAccountRecordVO();
                employeeAccountRecordVO.setEmployeeUid(employeeVO.getUid());
                employeeAccountRecordVO.setBusiType(EBusiType.employeeAccountLeave);
                employeeAccountRecordVO.setUid(null);
                employeeAccountRecordVO.setBusinessType(employeeAccountRecordVO.getBusiType().getType());
                employeeAccountRecordService.leaveAccount(employeeAccountRecordVO, adminVO);
            }
        }
        //课表停用

        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public Dept getDeptByUid(String gradeUid) {
        return gradeMapper.getDeptByUid(gradeUid);
    }

    @Override
    public String addClassByDept(GradeVO gradeVO) {
        String deptUid = gradeVO.getDeptUid();
        if (deptUid.contains(",")) {
            String[] deptUidArray = deptUid.split(",");
            for (String dept : deptUidArray) {
                if (!gradeService.verifyDept(dept)) {
                    Grade grade = new Grade();
                    grade.setDeptUid(dept);
                    grade.setStatus(EStatus.ENABLE);
                    grade.setBeginTime(new Date());
                    grade.insert();
                }
            }
        } else {
            if (!gradeService.verifyDept(deptUid)) {
                Grade grade = new Grade();
                grade.setDeptUid(deptUid);
                grade.setStatus(EStatus.ENABLE);
                grade.setBeginTime(new Date());
                grade.insert();
            }
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public boolean verifyDept(String deptUid) {
        QueryWrapper<Grade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_uid",deptUid);
        queryWrapper.eq(SQLConf.STATUS,EStatus.ENABLE);
        return gradeService.count(queryWrapper) > 0;
    }
}
